package net.starwifi.duobao.plugin.localVirtual;

import net.starwifi.duobao.entity.Payment;
import net.starwifi.duobao.entity.Refunds;
import net.starwifi.duobao.hfg.notify.NotifyTradeDetail;
import net.starwifi.duobao.hfg.query.QueryResponse;
import net.starwifi.duobao.hfg.refund.RefundResponse;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.plugin.PaymentPlugin;
import net.starwifi.duobao.service.PaymentService;
import net.starwifi.duobao.service.TradeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 宝币余额支付
 * Created by Administrator on 2016/1/13.
 */
@Component("localVirtualPayPlugin")
public class LocalVirtualPayPlugin extends PaymentPlugin {

    //private final static String baseRequestUrl="http://duob.tunnel.phpor.me/payment/return/";
   private final static String baseRequestUrl="http://127.0.0.1:8010/payment/return/";

    private String requestUrl;

    @Autowired
    TradeRecordService tradeRecordService;

    @Autowired
    PaymentService paymentService;

    @Override
    public String getName() {
        return "宝币支付";
    }

    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public String getAuthor() {
        return "ruoo";
    }

    @Override
    public String getSiteUrl() {
        return "http://duobao.com";
    }

    @Override
    public String getInstallUrl() {
        return null;
    }

    @Override
    public String getUninstallUrl() {
        return null;
    }

    @Override
    public String getSettingUrl() {
        return null;
    }

    @Override
    public String getRequestUrl() {
        return requestUrl;
    }

    @Override
    public RequestMethod getRequestMethod() {
        return RequestMethod.get;
    }

    @Override
    public String getRequestCharset() {
        return "UTF-8";
    }

    @Override
    public Map<String, Object> getParameterMap(Payment payment, String description, HttpServletRequest request) {
        requestUrl="";
        String paymentSn=payment.getSn();
        requestUrl=baseRequestUrl+paymentSn;
       // requestUrl=baseRequestUrl;
        if(payment.getStatus()== Payment.Status.wait){ //等待支付
            Boolean buyResult=tradeRecordService.buy(payment.getMemberId(), payment.getAmount(), description, payment.getOrderSn());
            if(buyResult){
                Payment dbPayment=paymentService.findBySn(payment.getSn());
                paymentService.handle(dbPayment);
            }
        }
        return null;
    }

    @Override
    public boolean verifyNotify(Payment payment, NotifyMethod notifyMethod, String data, HttpServletRequest request) {
        return false;
    }

    @Override
    public String getNotifyMessage(Payment payment, NotifyMethod notifyMethod,NotifyTradeDetail data, HttpServletRequest request) {

        return null;
    }

    @Override
    public Message refund(Refunds refunds, HttpServletRequest request) {
        return null;
    }

    /** 退款 */
    @Override
    public RefundResponse refunds(String sn, HttpServletRequest request) {return null;
    }

    @Override
    public Integer getTimeout() {
        return null;
    }

    public  QueryResponse query(Payment payment, HttpServletRequest request){
     return null;
    }
}
